package com.yanqu.road.server.cmd.activity.liondance;

import com.yanqu.road.entity.activity.liondance.config.LionDanceCardInfo;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.LionDanceProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.liondance.LionDanceModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.liondance.LionDanceMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = Protocol.S_LION_DANCE_GIVE_FRIEND, desc = "赠送")
public class LionDanceApplyHandleCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        // 没有活动
        if (!ActivityMgr.activityInTime(LionDanceMgr.getActivityInfo()) || LionDanceMgr.getConfig() == null) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }

        LionDanceProto.LionDanceApplyHandleReqMsg reqMsg = LionDanceProto.LionDanceApplyHandleReqMsg.parseFrom(packet.getBytes());

        if(player.getUserId() == reqMsg.getToUserId()){
            ret = GameErrorCode.E_LION_DANCE_UNABLE_GIVE_SELF;
            return;
        }
        LionDanceCardInfo cardInfo = LionDanceMgr.getConfig().getCardById(reqMsg.getCardId());
        if(cardInfo == null){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }
        //本服玩家
        if(reqMsg.getServerId() == GameServer.getInstance().getServerId()){
            //直接赠送
            ret = player.getModule(LionDanceModule.class).giveGift(reqMsg.getToUserId(), reqMsg.getApplyNo(), reqMsg.getCardId());
        }else {
            ret = player.getModule(LionDanceModule.class).crossGiveGift(reqMsg.getServerId(), reqMsg.getToUserId(), reqMsg.getApplyNo(), reqMsg.getCardId());
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_LION_DANCE_GIVE_FRIEND, LionDanceProto.LionDanceApplyHandleRespMsg.newBuilder().setRet(ret));
        }
    }
}
